// pages/index/collection/collection.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    collection:{},
    mediaList:[],
    bookmarkImage:'/assets/icons/bookmark_none.png',
    isBookmark:false,
    bookmarkId:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({id:wx.getStorageSync('collection').id})
    wx.removeStorageSync('collection')
    this.getData()
    this.isBookmark()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },
  //刷新
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
  },
  //网络请求，获取数据
  getData(){
    app.$http.get('/app/collection/list', {pageNum:1,pageSize:1,id:this.data.id}).then(res =>{
      this.setData({collection:res.data.records[0]})
      app.$http.get('/app/media/list', {pageNum:1,pageSize:50,collectionId:this.data.id}).then(res =>{
        this.setData({mediaList:res.data.records})
      })
      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }).catch(err => {
      console.log(err)
    })
  },
  isBookmark(){
    const userId = wx.getStorageSync('userId')
    const collectionId = this.data.id
    app.$http.get('/app/bookmark/list', {pageNum:1,pageSize:1,collectionId,userId}).then(res =>{
      if(res.data.total == 0){
        this.setData({
          isBookmark:false,
          bookmarkImage:'/assets/icons/bookmark_none.png'
        })
      }else{
        this.setData({
          isBookmark:true,
          bookmarkImage:'/assets/icons/bookmark.png',
          bookmarkId:res.data.records[0].id
        })
      }
    })
  },
  toMedia: function(e){
    const media = e.currentTarget.dataset.item;
    const index = e.currentTarget.dataset.index;
    wx.setStorageSync('collection',{id:this.data.id,mediaId:media.id,index:index});
    let url = ''
    switch(media.mediaType){
      case 0:
        url = '/pages/novel/novelByCollection/media'
        break;
      case 1:
        url = '/pages/comic/comicByCollection/media'
        break;
      case 2:
        url = '/pages/video/videoByCollection/media'
        break;
    }
    wx.navigateTo({
      url: url,
    })
  },
  bookmark(e){
    const userId = wx.getStorageSync('userId')
    const collectionId = this.data.id
    if(this.data.isBookmark){
      app.$http.remove('/app/bookmark/delete/'+this.data.bookmarkId).then(res=>{
        wx.showToast({
          title: '取消收藏',
          icon: 'none',
          duration:500
        });
        this.setData({
          isBookmark:false,
          bookmarkImage:'/assets/icons/bookmark_none.png'
        })
      })
    }else{
      app.$http.post('/app/bookmark/add',{userId,collectionId}).then(res=>{
        wx.showToast({
          title: '收藏成功',
          icon: 'none',
          duration:500
        });
        this.setData({
          isBookmark:true,
          bookmarkImage:'/assets/icons/bookmark.png'
        })
      })
    }
  }
})